package com.qingke.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingke.annotation.SkipSecurityCheck;
import com.qingke.pojo.Classes;
import com.qingke.pojo.Employee;
import com.qingke.pojo.EmployeeAccount;
import com.qingke.pojo.InClassTeacher;
import com.qingke.pojo.Student;
import com.qingke.pojo.StudentAccount;
import com.qingke.service.ClassService;
import com.qingke.service.EmployeeService;
import com.qingke.service.InClassTeacherService;
import com.qingke.service.StudentService;

@Controller
@RequestMapping("/teaCharge")
public class TeaChargeController {
	private static Logger logger = Logger.getLogger(TeaChargeController.class);
	private static final String password = "123456";
	private Map<String, Object> map;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClassService classService;
	@Autowired
	private InClassTeacherService inClassTeacherService;
	
	@RequestMapping("getTeaCharge")
	@SkipSecurityCheck
	@ResponseBody
	public Map getTeaCharge(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee teaCharge = (Employee) se.getAttribute("user");
		logger.info("get teaCharge:" + teaCharge);
		map.put("teaCharge", teaCharge);
		return map;
	}

	@RequestMapping("updateTeaChargeMessage")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateTeaChargeMessage(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		Employee teaCharge = (Employee) se.getAttribute("user");
		String userId = request.getParameter("user_id");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		logger.info("get guiderMessage[" + "userId=" + userId + ",email=" + email + ",phone=" + phone + "]");
		if (!userId.equals(teaCharge.getEmployeeNumber())) {
			map.put("isSame", "no");
		} else {
			teaCharge.setEmail(email);
			teaCharge.setPhone(phone);
			int count = employeeService.updateEmployeeByPrimaryKey(teaCharge);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "failed");
			}
		}
		Collection<Object> c = map.values();
		Iterator it = c.iterator();
		for (; it.hasNext();) {
			System.out.println(it.next());
		}
		return map;
	}

	@RequestMapping("updateTeaChargePassword")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateTeaChargePassword(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String oldPassword = request.getParameter("old_password");
		String newPassword = request.getParameter("new_password");
		String confrimPassword = request.getParameter("confirm_password");
		logger.info("get guiderPassword:old_password" + oldPassword + ",new_password=" + newPassword
				+ ",confirm_password=" + confrimPassword);
		HttpSession se = request.getSession();
		Employee teaCharge = (Employee) se.getAttribute("user");
		EmployeeAccount employeeAccount = employeeService.getEmployeeAccountById(teaCharge.getId());
		if (!oldPassword.equals(employeeAccount.getPassword())) {
			map.put("isExsit", "false");
		} else if (!newPassword.equals(confrimPassword)) {
			map.put("equal", "false");
		} else {
			employeeAccount.setPassword(newPassword);
			int count = employeeService.updateEmployeeAccountByPrimaryKey(employeeAccount);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "false");
			}
		}
		Collection<Object> c = map.values();
		Iterator it = c.iterator();
		for (; it.hasNext();) {
			System.out.println(it.next());
		}
		return map;
	}

	@RequestMapping("listPreviousStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map listPreviousStudent(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String className = request.getParameter("className");
		logger.info("get className=" + className);
		List<Student> student = new ArrayList();
		List<Student> students = studentService.listStudent();
		List<InClassTeacher> inClassTeacher = inClassTeacherService.listInClassTeacher();
		logger.info("list student:" + students);
		logger.info("list inClassTeacher:" + inClassTeacher);
		if (students != null) {
			if ("全部班级".equals(className)) {
				for (Student st : students) {
					logger.info("list student:" + st);
					if (!st.getIsDeleted()) {
						student.add(st);
					}
				}
				map.put("student", student);
			} else {
				for (Student st : students) {
					logger.info("list student:" + st);
					if (!st.getIsDeleted() && st.getClazz().getClassName().equals(className)) {
						student.add(st);
					}
				}
				map.put("student", student);
			}
			System.out.println(inClassTeacher!=null);
		} 
		if (inClassTeacher != null) {
			map.put("inClassTeacher", inClassTeacher);
		} else {
			System.out.println("123");
			map.put("student", "");
			map.put("inClassTeacher", "");
		}
		return map;
	}

	@RequestMapping("getStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map getStudent(HttpServletRequest request, ModelMap modelMap) {
		HttpSession se = request.getSession();
		map = new HashMap();
		String student_id = (String) request.getParameter("student_id");
		logger.info("get studentId:" + student_id);
		Student student = studentService.getStudentByStudentNumber(student_id);
		se.setAttribute("student", student);
		map.put("Student", student);
		return map;

	}

	@RequestMapping("updateStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateStudent(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String studentId = (String) request.getParameter("student_id");
		String name = (String) request.getParameter("name");
		String className = (String) request.getParameter("class_name");
		String email = (String) request.getParameter("email");
		String phone = (String) request.getParameter("phone");
		String teacherName = (String) request.getParameter("teacher_name");
		HttpSession se = request.getSession();
		Student st = (Student) se.getAttribute("student");
		logger.info("get student:stduentId=" + studentId + ",name=" + name + ",className=" + className + ",email="
				+ email + ",phone=" + phone + ",teacherName=" + teacherName);
		Student student = studentService.getStudentByStudentNumber(studentId);
		Classes clazz = classService.getClassByClassName(className);
		Employee teacher = employeeService.getEmployeeByName(teacherName);
		if (!studentId.equals(st.getStudentNumber())) {
			map.put("isSame", "no");
		} else if (clazz == null) {
			map.put("isClassExist", "no");
		} else if (teacher == null) {
			map.put("isTeacherExist", "no");
		} else if (!inClassTeacherService.isTeacherInClass(teacher.getId(), clazz.getId())) {
			map.put("teacherInClass", "no");
		} else {
			student.setName(name);
			student.setClassId(clazz.getId());
			student.setEmail(email);
			student.setPhone1(phone);
			int count = studentService.updateStudentByPrimaryKey(student);
			if (count > 0) {
				map.put("success", "true");
			} else {
				map.put("success", "true");
			}
		}
		return map;
	}

	@RequestMapping("listClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map listClass(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		List<Classes> clazz = classService.listClass();
		List<Classes> classes = new ArrayList();
		if (!clazz.isEmpty()) {
			for (Classes clas : clazz) {
				if (!clas.getIsDeleted()) {
					logger.info("get class=" + clas);
					classes.add(clas);
				}
			}
			map.put("classes", classes);
		} else {
			map.put("classes", "");
		}
		return map;
	}
	@RequestMapping("createStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map createStudent(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String studentId = (String) request.getParameter("student_id");
		String name = (String) request.getParameter("name");
		String className = (String) request.getParameter("class_name");
		String email = (String) request.getParameter("email");
		String phone = (String) request.getParameter("phone");
		String teacherName = (String) request.getParameter("teacher_name");
		logger.info("get student:stduentId=" + studentId + ",name=" + name + ",className=" + className + ",email="
				+ email + ",phone=" + phone + ",teacherName=" + teacherName);
		Classes clazz = classService.getClassByClassName(className);
		Employee teacher = employeeService.getEmployeeByName(teacherName);
		if (studentService.getStudentByStudentNumber(studentId) != null) {
			map.put("isExist", "yes");
		} else if (clazz == null) {
			map.put("isClassExist", "no");
		} else if (teacher == null) {
			map.put("isTeacherExist", "no");
		} else if (!inClassTeacherService.isTeacherInClass(teacher.getId(), clazz.getId())) {
			map.put("teacherInClass", "no");
		} else {
			Student student = new Student();
			StudentAccount studentAccount = new StudentAccount();
			student.setStudentNumber(studentId);
			student.setName(name);
			student.setClassId(clazz.getId());
			student.setEmail(email);
			student.setPhone1(phone);
			student.setLearnStatusId(1);
			student.setIsDeleted(false);
			int count = studentService.saveStudent(student);
			if (count > 0) {
				Student st = studentService.getStudentByStudentNumber(studentId);
				studentAccount.setId(st.getId());
				studentAccount.setPassword(password);
				int count1 = studentService.saveStudentAccount(studentAccount);
				if (count1 > 0) {
					map.put("success", "true");
				}
			} else {
				map.put("success", "true");
			}
		}
		return map;
	}
	  
}
